﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

using FITHOU_EDU_Server.Doituong;
using Newtonsoft.Json;
namespace FITHOU_EDU_Server.mobile
{
    public partial class ViewScore : System.Web.UI.Page
    {
        String sqlCon = ConfigurationManager.ConnectionStrings["FITHOU_EDU"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            getDiem();
        }
        private void getDiem()
        {
            SqlConnection Con = new SqlConnection(sqlCon);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter = null;
            DataTable tbl = new DataTable();
            string sUserName = "";
            int iKyHoc=0;
           try
            {
                sUserName = Request.QueryString["sUserName"];               
                String sKyHoc = Request.QueryString["iKyHoc"];
                iKyHoc = Int32.Parse(sKyHoc);
            
            }catch(Exception e)
            {
                Response.Write("Lỗi gán:"+e.Message+"          ");
            };
            
            if (sUserName == "" && iKyHoc== 0)
            {
                return;
            }
            try
            {
                Con.Open();
                cmd.CommandText = "sp_getDiembyID_Kyhoc";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = Con;
                cmd.Parameters.Add("@PK_sNguoidungID1", SqlDbType.Text).Value = sUserName;
                cmd.Parameters.Add("@iKyhoc1", SqlDbType.Int).Value=iKyHoc;              
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(tbl);               
                using (tbl)
                {
                    if (tbl.Rows.Count > 0)
                    {
                      String jsondiem = "";
                        Diem diem = new Diem();
                        Response.Write("{\"objdiems\":[");

                     for (int i = 0; i < tbl.Rows.Count; i++)
                        {
                            DataRow row = tbl.Rows[i];
                            diem.PK_iDiemID = Int32.Parse(row["PK_iDiemID"].ToString());
                            diem.fDiemchuyencan = float.Parse(row["fDiemchuyencan"].ToString());
                            diem.fDiemdieukien = float.Parse(row["fDiemdieukien"].ToString());
                            diem.fDiemthi = float.Parse(row["fDiemthi"].ToString());
                            diem.iLanthu = int.Parse(row["iLanthu"].ToString());
                            diem.iKyhoc = int.Parse(row["iKyhoc"].ToString());
                            diem.sMon =  row["sTenmonhoc"].ToString();
                            jsondiem = JsonConvert.SerializeObject(diem);
                            Response.Write(jsondiem);
                            if (i < tbl.Rows.Count - 1) Response.Write(",");

                           // row = null;
                           
                        }
                     Response.Write("]}");
                    // Response.Write(diem.ConvertToString());
                    // JSONObject dfsd;
                        
                    }
                    else
                    {
                        Response.Write("false");

                    }
                }
            }
            catch (Exception e)
            {
                Response.Write(e.Message);
            }
            finally
            {
                if (Con.State != ConnectionState.Closed)
                    Con.Close();
                adapter.Dispose();
                Con.Dispose();
            }
           
        }

        private String ConvertToString()
        {
            String rep = "";
            return rep;
        }

    }
}